package org.jeecg.modules.exKaibiao.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.exKaibiao.entity.ExBidOpeningRecord;
import org.jeecg.modules.exKaibiao.service.IExBidOpeningRecordService;
import org.jeecg.modules.exKaibiao.service.IExKaibiaoService;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.jeecg.modules.exTender.entity.ExTenderList;
import org.jeecg.modules.exTender.service.IExTenderListService;
import org.jeecg.modules.exTenderFileSetVariable.entity.ExTenderFileSetVariable;
import org.jeecg.modules.exTenderFileSetVariable.service.IExTenderFileSetVariableService;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExZbidOpeningArrange;
import org.jeecg.modules.exZbidOpeningArrange.service.IExZbidOpeningArrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExKaibiaoServiceImpl implements IExKaibiaoService {
    @Autowired
    private IExBidOpeningRecordService exBidOpeningRecordService;
    @Autowired
    private IExSectionService exSectionService;
    @Autowired
    private IExZbidOpeningArrangeService exZbidOpeningArrangeService;
    @Autowired
    private IExTenderListService tenderListService;
    @Autowired
    private IExTenderFileSetVariableService fileSetVariableService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExBidOpeningRecord kaibiaoStart(String sectionId) {
        ExBidOpeningRecord record = exBidOpeningRecordService.getBySectionId(sectionId);
        if (ObjectUtil.isNotEmpty(record)) {
            return record;
        }
        ExSection exSectionInfo = exSectionService.getById(sectionId);
        ExZbidOpeningArrange openingArrangeInfo = exZbidOpeningArrangeService.getBySectionId(sectionId);
        if (ObjectUtil.isEmpty(openingArrangeInfo)) {
            throw new JeecgBootException("没有找到该标段的场地预约信息");
        }
        Date decryptEndTime = DateUtil.offsetMinute(openingArrangeInfo.getBidOpeningTime(), 40);//开标时间 + 40分钟
        int compareResult = DateUtil.compare(new Date(), openingArrangeInfo.getBidOpeningTime());
        int compareResult2 = DateUtil.compare(new Date(), decryptEndTime);
        if (compareResult < 0) {
            throw new JeecgBootException("未到开标时间，" + DateUtil.formatDateTime(openingArrangeInfo.getBidOpeningTime()));
        }
        if (compareResult2 > 0) {
            throw new JeecgBootException("已过开标截止时间，" + DateUtil.formatDateTime(decryptEndTime));
        }
        List<ExTenderList> exTenderLists = tenderListService.queryBySectionId(sectionId);
        List<String> bidderNameList = exTenderLists.stream().map(ExTenderList::getBidderName).collect(Collectors.toList());
        String bidderNames = bidderNameList.stream().collect(Collectors.joining(","));
        ExBidOpeningRecord exBidOpeningRecord = new ExBidOpeningRecord();
        exBidOpeningRecord.setBidOpeningActor(bidderNames);
        exBidOpeningRecord.setBidSectionId(sectionId);
        exBidOpeningRecord.setStatus("1");
        exBidOpeningRecord.setBidOpeningTime(openingArrangeInfo.getBidOpeningTime());

        exBidOpeningRecord.setDecryptEndTime(decryptEndTime);
        exBidOpeningRecordService.save(exBidOpeningRecord);
        return exBidOpeningRecord;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void complete(String bidSectionId) {
        ExBidOpeningRecord record = exBidOpeningRecordService.getBySectionId(bidSectionId);
        if (ObjectUtil.isEmpty(record)) {
            throw new JeecgBootException("没有找到对应的开标记录");
        }
        if (ObjectUtil.notEqual(record.getStatus(), "1")) {
            throw new JeecgBootException("开标记录状态有误");
        }
        record.setStatus("2");
        record.setCompleteDate(new Date());
        exBidOpeningRecordService.updateById(record);
        ExSection exSection = exSectionService.getById(bidSectionId);
        exSection.setStatus("1");//标段状态为已开标
        exSection.setUpdateTime(new Date());
        exSectionService.updateById(exSection);
    }

    @Override
    public List<Map> deailList(String bidSectionId) {
        LambdaQueryWrapper<ExTenderList> query = new LambdaQueryWrapper<ExTenderList>().eq(ExTenderList::getBidSectionId, bidSectionId);
        List<ExTenderList> exTenderLists = tenderListService.list(query);
        List<Map> mapList = new ArrayList<>();
        exTenderLists.forEach(item -> {
            Map map = new HashMap();
            map.put("tenderListId", item.getId());
            map.put("bidderUserId", item.getBidderUserId());
            map.put("bidderName", item.getBidderName());
            map.put("bidPrice", item.getBidPrice());
            map.put("priceCurrency", item.getPriceCurrency());
            map.put("priceUnit", item.getPriceUnit());
            map.put("timeLimit", item.getTimeLimit());
            map.put("expand", expandList(bidSectionId,item.getId()));
            mapList.add(map);
        });
        return  mapList;
    }
    private  List<Map> expandList(String bidSectionId,String exTenderListId){
        List<ExTenderFileSetVariable> fileSetVariableList = fileSetVariableService.getBySectionId(bidSectionId);
        if(CollectionUtil.isEmpty(fileSetVariableList)){
            return null;
        }
        List<Map> mapList = new ArrayList<>();
        fileSetVariableList = fileSetVariableList.stream().filter(item -> item.getIfShow() == 1).collect(Collectors.toList());
        fileSetVariableList.forEach(item -> {
            Map map = new HashMap();
            map.put("columnKey",item.getColumnKey());
            map.put("columnName",item.getColumnName());
            map.put("columnValue","测试值");
            mapList.add(map);
        });
        return mapList;
    }
}
